<link type="text/css" rel="stylesheet" href="<?php echo $this->basePath('public/css/dbblack/css/ucenter.css'); ?>" />

<script type='text/javascript' src="<?php echo $this->basePath('public/css/dbblack/js/artdialog/artDialog.js?skin=simple'); ?>"></script>
<script type='text/javascript' src="<?php echo $this->basePath('public/css/dbblack/js/artdialog/plugins/iframeTools.js'); ?>"></script>
<link rel="stylesheet" type="text/css" href="<?php echo $this->basePath('public/css/dbblack/js/form/style.css'); ?>"/>
<script type='text/javascript' src="<?php echo $this->basePath('public/css/dbblack/js/form/form.js'); ?>"></script>
<script type='text/javascript' src="<?php echo $this->basePath('public/css/dbblack/js/jquery.form.js'); ?>"></script>

<div class="container clearfix">
  <?php echo $this->partial('shopfront/home/left_menu.phtml'); ?>
  <div class="content clearfix uc-content">
    <h1 class="title"><span><?php echo $this->translate('申请退货'); ?>：</span></h1>
    <?php if ($this->success_msg != '') { ?>
      <div class="message_success ie6png"><?php echo $this->success_msg; ?></div>
    <?php } ?>
    <div class="mt10" style="position:relative;">
      <form id="order_refund_form" name="order_refund_form" class="simple" method="post">
        <input type="hidden" name="hidden_order_sn" id="hidden_order_sn" value="">
        <table class="form">
          <tr>
            <td class="label"><?php echo $this->translate('订单编号'); ?><span class="must_add_value">*</span>：</td>
            <td>
              <input type="text" style="width: 300px;" id="order_sn" value="" name="order_sn" placeholder="<?php echo $this->translate('必须是已发货或者已收货的订单'); ?>">
              <button type="button" class="btn btn-small btn-primary" id="search-order" onclick="search_order_goods();"><?php echo $this->translate('点击查询'); ?></button>
            </td>
          </tr>
          <tr class="control-group order-goods" style="display: none;">
            <td class="label"><?php echo $this->translate('退货商品'); ?><span class="must_add_value">*</span>：</td>
            <td id="order-goods-list"></td>
          </tr>
          <tr>
            <td class="label"><?php echo $this->translate('退款方式'); ?><span class="must_add_value">*</span>：</td>
            <td>
              <label class="radio">
                <input type="radio" name="refund_type" value="1" onclick="select_radio(1);" checked><?php echo $this->translate('退款到账户余额'); ?>
              </label>
              <label class="radio">
                <input type="radio" name="refund_type" value="2"  onclick="select_radio(2);"><?php echo $this->translate('退款到银行卡'); ?>
              </label>
              <label class="radio">
                <input type="radio" name="refund_type" value="3"  onclick="select_radio(3);"><?php echo $this->translate('退款到第三方支付'); ?>
              </label>
            </td>
          </tr>
          <tr class="refund-bank" style="display: none;">
            <td class="label"><?php echo $this->translate('开户行'); ?><span class="must_add_value">*</span>：</td>
            <td><input type="text" style="width: 300px;" id="bank_name" value="" name="bank_name"></td>
          </tr>
          <tr class="refund-bank" style="display: none;">
            <td class="label"><?php echo $this->translate('姓名'); ?><span class="must_add_value">*</span>：</td>
            <td><input type="text" style="width: 300px;" id="bank_account" value="" name="bank_account"></td>
          </tr>
          <tr class="refund-bank" style="display: none;">
            <td class="label"><?php echo $this->translate('收款卡号'); ?><span class="must_add_value">*</span>：</td>
            <td><input type="text" style="width: 300px;" id="bank_card_number" value="" name="bank_card_number"></td>
          </tr>

          <tr class="refund-pay" style="display: none;">
            <td class="label"><?php echo $this->translate('支付名称'); ?><span class="must_add_value">*</span>：</td>
            <td><input type="text" style="width: 300px;" id="pay_name" value="" name="pay_name"></td>
          </tr>
          <tr class="refund-pay" style="display: none;">
            <td class="label"><?php echo $this->translate('姓名'); ?><span class="must_add_value">*</span>：</td>
            <td><input type="text" style="width: 300px;" id="pay_user_name" value="" name="pay_user_name"></td>
          </tr>
          <tr class="refund-pay" style="display: none;">
            <td class="label"><?php echo $this->translate('收款帐号'); ?><span class="must_add_value">*</span>：</td>
            <td><input type="text" style="width: 300px;" id="pay_account" value="" name="pay_account"></td>
          </tr>

          <tr>
            <td class="label"><?php echo $this->translate('退货原因'); ?><span class="must_add_value">*</span>：</td>
            <td><textarea class="span6" style="height: 60px;" name="refund_info" id="refund_info" placeholder="<?php echo $this->translate('最多100字'); ?>"></textarea></td>
          </tr>

          <tr>
            <td colspan="2" class="tc">
              <input type="button" class="btn btn-primary" onclick="order_refund_save();" value="<?php echo $this->translate('申请退货'); ?>">
              &nbsp;&nbsp;&nbsp;&nbsp;
              <button class="btn" onclick="location.href='<?php echo $this->url('frontrefund/default'); ?>'"><?php echo $this->translate('返回'); ?></button>
            </td>
          </tr>
        </table>
      </form>
    </div>
  </div>
</div>
<script>
  function order_refund_save() {
    if($('#order_sn').val() == '') {
      art.dialog.tips("<p class='warning'><?php echo $this->translate('订单编号不能为空！'); ?></p>");
      return false;
    }
    if($('#refund_info').val() == '') {
      art.dialog.tips("<p class='warning'><?php echo $this->translate('退货原因不能为空！'); ?></p>");
      return false;
    }

    var hidden_order_sn = $('#hidden_order_sn').val();
    if(hidden_order_sn != '') {
      var refund_type = $('input[name="refund_type"]:checked').val();
      var bank_name   = $.trim($('#bank_name').val());
      var bank_account= $.trim($('#bank_account').val());
      var bank_card_number= $.trim($('#bank_card_number').val());
      var pay_name    = $.trim($('#pay_name').val());
      var pay_account = $.trim($('#pay_account').val());
      var pay_user_name = $.trim($('#pay_user_name').val());

      if(refund_type == 2) {
        if(bank_name == '' || bank_account == '' || bank_card_number == '') {
          art.dialog.tips("<p class='warning'><?php echo $this->translate('开户行 姓名 收款卡号 不能为空！'); ?></p>");
          return ;
        }
      }
      if(refund_type == 3) {
        if(pay_name == '' || pay_account == '' || pay_user_name == '') {
          art.dialog.tips("<p class='warning'><?php echo $this->translate('支付名称 姓名 收款账号 不能为空！'); ?></p>");
          return ;
        }
      }

      $.post("<?php echo $this->url('frontrefund/default', array('action'=>'saveRefund')); ?>",$('#order_refund_form').formSerialize(),
          function(data){
            if(data == 'true') {
              art.dialog.tips("<p class='warning'><?php echo $this->translate('退货申请提交成功！'); ?></p>");
              window.location.href= '<?php echo $this->url('frontrefund/default'); ?>';
              return false;
            } else {
              alert(data);
              return false;
            }
          });

    }else {
      art.dialog.tips("<p class='warning'><?php echo $this->translate('需要 点击查询 按钮，添加退货商品！'); ?></p>");
      return false;
    }
  }

  function select_radio(refund_type) {
    if(refund_type == 1){
      $(".refund-bank").css('display', 'none');
      $(".refund-pay").css('display', 'none');
    }
    if(refund_type == 2) {
      $(".refund-pay").css('display', 'none');
      $(".refund-bank").css('display', '');
    }
    if(refund_type == 3) {
      $(".refund-bank").css('display', 'none');
      $(".refund-pay").css('display', '');
    }
  }

  function search_order_goods() {
    var order_sn = $('#order_sn').val();
    var hidden_order_sn = $('#hidden_order_sn').val();

    if(order_sn == '') {
      art.dialog.tips("<p class='warning'><?php echo $this->translate('订单编号不能为空！'); ?></p>");
      return ;
    }
    if(hidden_order_sn != '') {//修改操作
      $('#order_sn').removeAttr('readonly');
      $('#search-order').text('<?php echo $this->translate('点击查询'); ?>');
      $('#search-order').addClass('btn-primary');
      $('#order-goods-list').html('');
      $('.order-goods').css('display', 'none');
      $('#hidden_order_sn').val('');
      return ;
    }

    $.post("<?php echo $this->url('frontrefund/default', array('action'=>'searchOrderGoods')); ?>",{order_sn:order_sn},
        function(data){
          if(data.state == 'true') {
            var i,html = '';
            for(i=0; i<data.goods.length; i++) {
              html += '<label class="checkbox"><input type="checkbox" name="goods_id['+data.goods[i].order_goods_id+']" value="'+data.goods[i].order_goods_id+'" checked>'+data.goods[i].goods_name+data.goods[i].goods_extend_info+'</label>';
            }
            $('#order_sn').attr('readonly', 'readonly');
            $('#search-order').text('<?php echo $this->translate('修改编号'); ?>');
            $('#search-order').removeClass('btn-primary');
            $('#hidden_order_sn').val(order_sn);
            //$('#order_sn').removeAttr('readonly');

            $('.order-goods').css('display', '');
            $('#order-goods-list').html(html);
          } else {
            alert(data.message);
            $('#order_sn').val('');
            return false;
          }
        },'json');
  }
</script>